home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / oper_sys / emerald / emrldsys.lha / Language / Compiler / readTree.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-08-16  |  3.8 KB  |  117 lines

  1. /*
  2.  * @(#)readTree.c    1.2  3/18/87
  3.  */
  4. #include "assert.h"
  5. #include "nodes.h"
  6. #include "symbols.h"
  7. #include "system.h"
  8. #include "version.h"
  9. #include "error.h"
  10. #include <a.out.h>
  11.  
  12. #undef VERBOSE
  13.  
  14. NodePtr readTree(fileName)
  15. char *fileName;
  16. {
  17.     FILE *inf;
  18.     struct exec h;
  19.     int stsize;
  20.     char *text, *data;
  21.     struct relocation_info *treloc, *dreloc;
  22.     struct nlist *syms;
  23.     static struct sts {
  24.     int size;
  25.     char strings[20];
  26.     } *stringTable;
  27.     register int i;
  28.     register struct relocation_info *rl;
  29.     int numRelocations;
  30.     
  31.     inf = fopen(fileName, "r");
  32.     if (inf == NULL) {
  33.       fprintf(stderr, "Can't open file \"%s\"\n", fileName);
  34.       return (NULL);
  35.     }
  36.     if (fread((char *)&h, sizeof(h), 1, inf) != 1) assert(FALSE);
  37. #ifdef VERBOSE
  38.     fprintf(stdout, "magic = 0%o\n", h.a_magic);
  39.     fprintf(stdout, "text = %d\n", h.a_text);
  40.     fprintf(stdout, "data = %d\n", h.a_data);
  41.     fprintf(stdout, "bss = %d\n", h.a_bss);
  42.     fprintf(stdout, "syms = %d\n", h.a_syms);
  43.     fprintf(stdout, "entry = 0x%x\n", h.a_entry);
  44.     fprintf(stdout, "trsize = %d\n", h.a_trsize);
  45.     fprintf(stdout, "drsize = %d\n", h.a_drsize);
  46.     troff = N_TXTOFF(h)+h.a_text+h.a_data;
  47.     droff = troff + h.a_trsize;
  48.     fprintf(stdout, "troff = %d\n", troff);
  49.     fprintf(stdout, "droff = %d\n", droff);
  50.     fprintf(stdout, "symoff = %d\n", N_SYMOFF(h));
  51.     fprintf(stdout, "stroff = %d\n", N_STROFF(h));
  52. #endif
  53.     text = (char *) malloc((unsigned)h.a_text);
  54.     data = (char *) malloc((unsigned)h.a_data);
  55.     treloc = (struct relocation_info *) malloc ((unsigned)h.a_trsize);
  56.     dreloc = (struct relocation_info *) malloc ((unsigned)h.a_drsize);
  57.     syms = (struct nlist *) malloc ((unsigned)h.a_syms);
  58.  
  59.     if (h.a_magic != OMAGIC) 
  60.       if (fseek(inf, (long)N_TXTOFF(h), 0) == -1) assert(FALSE);
  61.  
  62.     if (h.a_text != 0) if (fread(text, (int)h.a_text, 1, inf) != 1) assert(FALSE);
  63.     if (h.a_data != 0) if (fread(data, (int)h.a_data, 1, inf) != 1) assert(FALSE);
  64.     if (h.a_trsize != 0) if (fread((char *)treloc, (int)h.a_trsize, 1, inf) != 1) assert(FALSE);
  65.     if (h.a_drsize != 0) if (fread((char *)dreloc, (int)h.a_drsize, 1, inf) != 1) assert(FALSE);
  66.     if (h.a_syms != 0) if (fread((char *)syms, (int)h.a_syms, 1, inf) != 1) assert(FALSE);
  67.  
  68.     if (fread((char *)&stsize, sizeof(int), 1, inf) != 1) assert(FALSE);
  69. #ifdef VERBOSE
  70.     fprintf(stdout, "stsize = %d\n", stsize);
  71. #endif
  72.     stringTable = (struct sts *) malloc((unsigned)stsize);
  73.     stringTable->size = stsize;
  74.     if (fread(stringTable->strings, stsize - 4, 1, inf) != 1) assert(FALSE);
  75.     if (fclose(inf) == EOF) assert(FALSE);
  76.     assert(h.a_text == 0);
  77.     assert(h.a_trsize == 0);
  78.     if (*(int *) data != TREEMAGIC || *(int *)(data + 4) != TREEVERSION) {
  79.       fprintf(stderr, "Old version tree, see norm.\n");
  80.       numberOfSemanticErrors ++;
  81.       free(text);
  82.       free((char *)treloc);
  83.       free((char *)dreloc);
  84.       free((char *)syms);
  85.       free((char *)stringTable);
  86.       return (NULL);
  87.     }
  88. #ifdef VERBOSE
  89.     fprintf(stdout, "Data relocation\n");
  90. #endif
  91.     numRelocations = h.a_drsize / sizeof (struct relocation_info);
  92.     for (i = 0; i < numRelocations; i++) {
  93.     rl = &dreloc[i];
  94. #ifdef VERBOSE
  95.     fprintf(stdout, "%3d addr 0x%x sym %d pcrel %d len %d ext %d val %u\n",
  96.         i, rl->r_address, rl->r_symbolnum, rl->r_pcrel, 
  97.         rl->r_length, rl->r_extern,
  98.         rl->r_length == 0 ? *(&data[rl->r_address]) :
  99.         rl->r_length == 1 ? *((short *)(&data[rl->r_address])) :
  100.         *((long *)(&data[rl->r_address])));
  101. #endif
  102.     /* actually do the relocation */
  103.     assert(rl->r_length == 2);
  104.     assert(rl->r_extern == 0);
  105.     assert(rl->r_pcrel == 0);
  106.     assert(rl->r_symbolnum == N_DATA);
  107.     *(long *)(&data[rl->r_address]) += (long) data;
  108.     }
  109.     free(text);
  110.     free((char *)treloc);
  111.     free((char *)dreloc);
  112.     free((char *)syms);
  113.     free((char *)stringTable);
  114.     
  115.     return ((NodePtr) (data + 12));
  116. }
  117.